package org.mzyyn.qr.service.user.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.mzyyn.qr.context.UserAuthContext;
import org.mzyyn.qr.dto.user.Code2sessionLoginDTO;
import org.mzyyn.qr.dto.user.UserProfileLoginDTO;
import org.mzyyn.qr.dto.user.UserRegisterInfoDTO;
import org.mzyyn.qr.entity.UserPO;
import org.mzyyn.qr.feign.qq.QqRpcService;
import org.mzyyn.qr.feign.qq.vo.JscodeSessionVO;
import org.mzyyn.qr.mapper.UserMapper;
import org.mzyyn.qr.service.user.UserAppletsService;
import org.mzyyn.qr.vo.user.SessionInfoVO;
import org.qr.xiaocao.common.enums.CommonResultEnum;
import org.qr.xiaocao.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lipeng
 * @since 2024-06-12
 */
@Service
@Slf4j
public class UserH5AppletsServiceImpl extends ServiceImpl<UserMapper, UserPO> implements UserAppletsService {

    @Override
    public UserPO findByUser(String h5openid) {
        if (StrUtil.isEmpty(h5openid)) {
            return null;
        }
        LambdaQueryWrapper<UserPO> userQuery = new LambdaQueryWrapper<>();
        userQuery.eq(UserPO::getH5Openid, h5openid);
        return this.baseMapper.selectOne(userQuery);
    }

    @Override
    public UserPO checkAndGetUserByLoginPhone() {
        UserPO user = findByUser(UserAuthContext.getUserInfo().getH5Openid());
        if (null == user) {
            throw new ServiceException(CommonResultEnum.DATA_NOT_BELONG.getCode(), "当前用户不存在");
        }
        return user;
    }

    @Override
    public SessionInfoVO appletGetSessionByCode(Code2sessionLoginDTO code2sessionLoginDTO) {
      return null;
    }

    @Override
    public UserRegisterInfoDTO getRegisterInfo(UserProfileLoginDTO userProfileLoginDTO) {

        UserRegisterInfoDTO userRegisterInfoDTO = new UserRegisterInfoDTO();
        userRegisterInfoDTO.setPhone(StrUtil.EMPTY);
        userRegisterInfoDTO.setUnionid(StrUtil.EMPTY);
        userRegisterInfoDTO.setQqopenid(StrUtil.EMPTY);
        userRegisterInfoDTO.setH5openid(userProfileLoginDTO.getCode());
        userRegisterInfoDTO.setWxopenid(StrUtil.EMPTY);
        userRegisterInfoDTO.setUserName(userProfileLoginDTO.getUserName());
        userRegisterInfoDTO.setAvatar(userProfileLoginDTO.getAvatar());

        return userRegisterInfoDTO;

    }

}
